package de.recipeminer.ui.commands;

import com.google.common.base.Optional;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.ImmutableSet;
import de.recipeminer.ui.commands.listing.ListRecipesCommand;
import de.recipeminer.ui.commands.listing.ListStopwordsCommand;
import de.recipeminer.ui.commands.workflow.ReadStopWordsCommand;
import org.apache.log4j.Logger;

/**
 * @autor: Markus Ackermann
 */
public class CommandRegistry {
    private static Logger logger = Logger.getLogger(CommandRegistry.class);
    private BiMap<String, UserCommand> registeredCommands = HashBiMap.create();
    
    private static CommandRegistry instance;

    public CommandRegistry() {
        registerCommands();
    }

    public static CommandRegistry getInstance() {
        if (instance == null) {
            instance = new CommandRegistry();
        } return instance;
    }

    public ImmutableSet<UserCommand> getKnownCommands() {
        ImmutableSet.Builder<UserCommand> builder = ImmutableSet.builder();
        return builder.addAll(registeredCommands.values()).build();
    }
    
    
    public ImmutableSet<String> getKnownCommandNames() {
        ImmutableSet.Builder<String> builder = ImmutableSet.builder();
        return builder.addAll(registeredCommands.keySet()).build();
    }


    /**
     * Register new Commands here.
     */
    private void registerCommands() {
        registeredCommands.put(ListRecipesCommand.NAME, new ListRecipesCommand());
        registeredCommands.put(ReadStopWordsCommand.NAME, new ReadStopWordsCommand());
        registeredCommands.put(ListStopwordsCommand.NAME, new ListStopwordsCommand());
    }

    public Optional<UserCommand> getCommand(String requestedCommand) {
        return Optional.fromNullable(registeredCommands.get(requestedCommand));
    }
}
